package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
@Slf4j
@Api(tags = "04. 属性模板管理模块")
@Validated
@RestController
@RequestMapping("/attribute-templates")
public class AttributeTemplateController {

    @Autowired
    IAttributeTemplateService attributeTemplateService;

    @ApiOperation("添加模板属性")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid AttributeTemplateAddNewDTO attributeTemplateAddNewDTO){
        attributeTemplateService.addNew(attributeTemplateAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除属性模板")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性模板id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1, message = "删除属性模板失败，尝试删除的属性模板的ID无效！") @PathVariable Long id){
        log.debug("开始处理【删除属性模板】的请求，参数：{}", id);
        attributeTemplateService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改属性模板基本信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateInfoById(@PathVariable Long id, AttributeTemplateUpdateDTO attributeTemplateUpdateDTO){
        log.debug("开始处理【修改属性模板基本信息】的请求，参数ID：{}, 新数据：{}", id, attributeTemplateUpdateDTO);
        attributeTemplateService.updateInfoById(id,attributeTemplateUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "属性模板id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据id查询属性模板详情】的请求，参数：{}", id);
        AttributeTemplateStandardVO attributeTemplate = attributeTemplateService.getStandardById(id);
        return JsonResult.ok(attributeTemplate);
    }

    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult list(){
        log.debug("开始处理【查询属性模板列表】的请求，参数：无");
        List<AttributeTemplateListItemVO> list = attributeTemplateService.list();
        return JsonResult.ok(list);
    }
}
